package cn.yeyiu.sexyiu.activity;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import java.util.List;

import cn.yeyiu.sexyiu.R;
import cn.yeyiu.sexyiu.adapter.RandomVideoAdapter;
import cn.yeyiu.util.bean.VideoInfo;

public class RandomVideoActivity extends AppCompatActivity {

    private ImageView back_button;
    private SwipeRefreshLayout swipeLa;
    private RecyclerView rv;

    private RandomVideoAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_random_video);

        bindView();
        initUI();
        bindEvent();
        refresh();
    }

    void refresh(){
        adapter.updateVideo();
    }

    void bindView(){
        back_button = (ImageView) findViewById(R.id.back_btn);
        swipeLa =findViewById(R.id.swipeLa);
        rv= findViewById(R.id.random_video_rv);
    }

    void bindEvent(){
        back_button.setOnClickListener(v -> {
            finish();
        });
        swipeLa.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        adapter.dataListLd.observe(this, new Observer<List<VideoInfo>>() {
            @Override
            public void onChanged(List<VideoInfo> videoInfos) {
                adapter.notifyDataSetChanged();
                swipeLa.setRefreshing(false);
            }
        });
    }

    void initUI(){
        TextView textView = findViewById(R.id.back_tv);
        textView.setText("随机推荐");

        RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        rv.setLayoutManager(layoutManager);
        adapter = new RandomVideoAdapter(this);
        rv.setAdapter(adapter);

        swipeLa.setColorSchemeResources(R.color.main_color);
    }
}